package br.com.evl.tecinfo.gui;

import br.com.evl.tecinfo.dao.Funcionario_Banco;
import br.com.evl.tecinfo.modelo.Funcionario;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author alysson
 */
public class TelaSelecionarFuncionario extends javax.swing.JFrame {

    Funcionario_Banco funcionarioBD;
    Funcionario funcionario;

    TelaLogin2 telaLogin;

    public TelaSelecionarFuncionario() {
        initComponents();

        atualizarTabela();
        funcionario = new Funcionario();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbFuncionarios = new javax.swing.JTable();
        jbtnSelecionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jtbFuncionarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtbFuncionarios);

        jbtnSelecionar.setText("Selecionar");
        jbtnSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSelecionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(319, 319, 319)
                .addComponent(jbtnSelecionar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jbtnSelecionar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSelecionarActionPerformed
        if (jtbFuncionarios.getSelectedRow() != -1) {
            setVisible(false);

            funcionario.setNome((String) jtbFuncionarios.getValueAt(jtbFuncionarios.getSelectedRow(), 0));
            funcionario.setCpf((String) jtbFuncionarios.getValueAt(jtbFuncionarios.getSelectedRow(), 3));
            funcionario.setValorHora(Double.valueOf((String) jtbFuncionarios.getValueAt(jtbFuncionarios.getSelectedRow(), 5)));

            telaLogin.telaHoraExtra.jlblFuncionario.setText(funcionario.getNome());

            telaLogin.telaHoraExtra.novoregistroHora.setFuncionario(funcionario);
        } else {

            System.out.println("selecione a tabela");
        }
    }//GEN-LAST:event_jbtnSelecionarActionPerformed

    private void atualizarTabela() {

        funcionarioBD = new Funcionario_Banco();

        DefaultTableModel dtm = new DefaultTableModel(null, new String[]{"Nome", "Cargo", "Forma de Pag.", "CPF", "Área", "Valor da Hora", "Conta", "Ag", "Banco"});

        try {
            while (funcionarioBD.consulta.next()) {

                Object[] opcoes = {funcionarioBD.consulta.getString("nome"), funcionarioBD.consulta.getString("cargo"),
                    funcionarioBD.consulta.getString("formaDePagamento"), funcionarioBD.consulta.getString("cpf"),
                    funcionarioBD.consulta.getString("area"), funcionarioBD.consulta.getString("valorDaHora"),
                    funcionarioBD.consulta.getString("conta"), funcionarioBD.consulta.getString("agencia"),
                    funcionarioBD.consulta.getString("banco")};
                dtm.addRow(opcoes);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TelaCadastroFormaPagamento.class.getName()).log(Level.SEVERE, null, ex);
        }
        jtbFuncionarios.setModel(dtm);

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnSelecionar;
    private javax.swing.JTable jtbFuncionarios;
    // End of variables declaration//GEN-END:variables
}
